//
//  StepProgressView.swift
//  SCA
//
//  Created by tjun on 2025/9/23.
//

import UIKit
import SnapKit

class StepView: UIView {
    
    let imageView = UIImageView(image: UIImage(named: "check_gradient"))
    let textLabel = UILabel(font: UIFontSemiboldMake(20), textColor: .gray, textAlignment: .center)
    let step: Int
    var isComplete: Bool = false {
        didSet {
            if isComplete {
                imageView.isHidden = false
                textLabel.isHidden = true
            } else {
                imageView.isHidden = true
                textLabel.isHidden = false
            }
        }
    }
    
    init(step: Int) {
        self.step = step
        super.init(frame: .zero)
        self.setupUI()
    }
    
    func setupUI() {
        
        imageView.addTo(self)
        imageView.isHidden = true
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        textLabel.addTo(self)
        textLabel.text = "\(step)"
        textLabel.isHidden = false
        textLabel.layer.borderColor = UIColorMake(0xC5C5C5).cgColor
        textLabel.layer.borderWidth = 1
        textLabel.layer.masksToBounds = true
        textLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        textLabel.layer.cornerRadius = frame.width * 0.5
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class StepProgressView: UIView {
    
    var currentStep: Int = 0 {
        didSet {
            for i in 0...stepViews.count - 1 {
                if i <= currentStep - 1 {
                    stepViews[i].isComplete = true
                } else {
                    stepViews[i].isComplete = false
                }
            }
            progressLabel.text = String(format: "进度: %d/3".localizedString, currentStep)
        }
    }
    
    private var stepViews: [StepView] = []
    let titleLabel = UILabel(font: UIFontMake(14), textColor: .white)
    private let progressLabel = UILabel(font: UIFontMediumMake(12), textColor: UIColorMake(0xC5C5C5))

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
        
    private func setupUI() {
        
        let titleView = UIView().addTo(self)
        titleView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(46)
        }
        
        titleLabel.addTo(titleView)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        progressLabel.addTo(titleView)
        progressLabel.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        let progressView = UIView().addTo(self)
        progressView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom)
        }
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.spacing = 10
        stackView.alignment = .center
        progressView.addSubview(stackView)
        
        let step1View = StepView(step: 1)
        
        let line1View = UIView()
        line1View.backgroundColor = UIColorMake(0xC5C5C5)
        
        let line2View = UIView()
        line2View.backgroundColor = UIColorMake(0xC5C5C5)
        
        let step2View = StepView(step: 2)
        
        let step3View = StepView(step: 3)
        
        stackView.addArrangedSubview(step1View)
        stackView.addArrangedSubview(line1View)
        stackView.addArrangedSubview(step2View)
        stackView.addArrangedSubview(line2View)
        
        stackView.addArrangedSubview(step3View)
        
        step1View.snp.makeConstraints { make in
            make.width.height.equalTo(40)
        }
        
        line1View.snp.makeConstraints { make in
            make.width.equalTo(64)
            make.height.equalTo(1)
        }
        
        step2View.snp.makeConstraints { make in
            make.width.height.equalTo(40)
        }
        
        line2View.snp.makeConstraints { make in
            make.width.equalTo(64)
            make.height.equalTo(1)
        }
        
        step3View.snp.makeConstraints { make in
            make.width.height.equalTo(40)
        }
        
        stackView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.height.equalTo(42)
        }
        
        stepViews.append(step1View)
        stepViews.append(step2View)
        stepViews.append(step3View)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
